using System;
using System.Data;
using System.Windows.Forms;
using Company.BLL.DuLieuChuan;
using Company.BLL.SXXK;
using Janus.Windows.GridEX;

namespace Company.Interface.SXXK
{
    public partial class NguyenPhuLieuRegistedForm : BaseForm
    {
        public NguyenPhuLieu NguyenPhuLieuSelected = new NguyenPhuLieu();
        
        private DataSet dsRegistedList;
        public NguyenPhuLieuRegistedForm()
        {
            InitializeComponent();
        }

        public void BindData()
        {
            dgList.DataSource = new NguyenPhuLieu().SearchBy_MaHaiQuan(ctrDonViHaiQuan.Ma).Tables[0];
        }

        //-----------------------------------------------------------------------------------------
        
        private void khoitao_DuLieuChuan()
        {
            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();
            
            if (this.CalledForm == "DinhMucSendForm")
            {
                ctrDonViHaiQuan.Enabled = false;
            }
            
            lblHint.Visible = this.CalledForm != string.Empty;
        }

        //-----------------------------------------------------------------------------------------
        
        private void NguyenPhuLieuRegistedForm_Load(object sender, EventArgs e)
        {
            this.khoitao_DuLieuChuan();
            // Nguyên phụ liệu đã đăng ký.
            this.BindData();
            // Doanh nghiệp / Đại lý TTHQ.
            dgList.Tables[0].Columns["MaDoanhNghiep"].Visible = GlobalSettings.DAI_LY_TTHQ;
        }

        //-----------------------------------------------------------------------------------------
        
        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
                e.Row.Cells["DVT_ID"].Text = this.DonViTinh_GetName(e.Row.Cells["DVT_ID"].Value.ToString());
        }

        //-----------------------------------------------------------------------------------------
        
        private void btnGetListFromHQ_Click(object sender, EventArgs e)
        {
            try
            {
                WSForm wsForm = new WSForm();
                wsForm.ShowDialog(this);
                if (!wsForm.IsReady) return;
                this.Cursor = Cursors.WaitCursor;
                this.dsRegistedList = new NguyenPhuLieu().WS_GetDanhSachDaDangKy(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
                dgList.DataSource = this.dsRegistedList.Tables[0];
                // Cập nhật vào CSDL.
                bool ret = new NguyenPhuLieu().UpdateRegistedToDatabase(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI, this.dsRegistedList);
                this.Cursor = Cursors.Default;
                if (ret)
                {
                    this.ShowMessage("Cập nhật thành công", false);
                    BindData();
                }
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                this.ShowMessage("Có lỗi: " + ex.Message, false);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        //-----------------------------------------------------------------------------------------
        
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (lblHint.Visible)
            {
                if (e.Row.RowType == RowType.Record)
                {
                    string maNPL = e.Row.Cells["Ma"].Text;
                    this.NguyenPhuLieuSelected.Ma = maNPL;
                    this.NguyenPhuLieuSelected.MaHaiQuan = this.MaHaiQuan;
                    this.NguyenPhuLieuSelected.MaDoanhNghiep = GlobalSettings.DoanhNghiep.Ma;
                    this.NguyenPhuLieuSelected.Ten = e.Row.Cells["Ten"].Text;
                    this.NguyenPhuLieuSelected.MaHS = e.Row.Cells["MaHS"].Text;
                    this.NguyenPhuLieuSelected.DVT_ID = e.Row.Cells["DVT_ID"].Text;
                    this.NguyenPhuLieuSelected.Load();
                    this.Close();
                }
            }
            else
            {
  
            }
        }

        private void ctrDonViHaiQuan_ValueChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //-----------------------------------------------------------------------------------------
    }
}